package cn.com.darly.videoandroidpm.ui.presenter;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.darly.common.ToastApp;
import com.darly.common.retrofit.FrameListener;

import java.util.LinkedHashMap;
import java.util.Map;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseApplication;
import cn.com.darly.videoandroidpm.bean.BaseBean;
import cn.com.darly.videoandroidpm.common.EncryperAES;
import cn.com.darly.videoandroidpm.ui.RegisterActivity;
import cn.com.darly.videoandroidpm.ui.RegisterNextActivity;
import cn.com.darly.videoandroidpm.ui.biz.RegisterListener;
import cn.com.darly.videoandroidpm.widget.shapeloading.ShapeLoadingDialog;


/**
 * @author zhangyh2 s 上午10:57:39 TODO 控制器 页面动作操作
 */
public class RegisterPresenter {

    private RegisterListener userBiz;
    private RegisterListener.RegisterInterface main;

    private ShapeLoadingDialog loading;


    public RegisterPresenter(RegisterListener.RegisterInterface main) {
        this.main = main;
        this.userBiz = new RegisterListener();
        loading = new ShapeLoadingDialog.Builder((RegisterActivity)main)
                .loadText("请求中...")
                .build();
    }


    /**
     * @param context
     * @param v       TODO点击事件在这里进行处理
     */
    public void onClickDown(Context context, View v) {
        switch (v.getId()) {
            case R.id.title_view_back_img:
                main.end();
                break;
            case R.id.id_register_btn:
                //到服务端获取验证码
                if (EncryperAES.isChinaPhoneLegal(main.getTel())){
                    Map<String, String> params = new LinkedHashMap<String, String>();
                    params.put("deviceid", BaseApplication.getInstance().getDevId());
                    params.put("tel", main.getTel());
                    check(params);
                }else {
                    ToastApp.showToast("手机号码异常，请重新输入");
                }
                break;
            case R.id.title_view_ok:
                //在这里验证输入码
                if (main.isCodeRight()){
                    Intent intent = new Intent(context, RegisterNextActivity.class);
                    intent.putExtra("title",R.string.register_title);
                    intent.putExtra("tel",main.getTel());
                    context.startActivity(intent);
                    main.end();
                }else {
                    ToastApp.showToast("验证码错误，请检查验证码");
                }
                break;
            default:
                break;
        }
    }


    public void check(Map<String, String> params) {
        userBiz.onStart(loading);
        main.setDisableClick();
        userBiz.onCheck(params, new FrameListener<BaseBean<String>>() {
            @Override
            public void onSucces(BaseBean<String> info) {
                main.setEnableClick();
                userBiz.onStop(loading);
                main.onCheckSuccess(info.getData());

            }

            @Override
            public void onFaild(int code, String info) {
                main.setEnableClick();
                userBiz.onStop(loading);
                if (code == 0){
                    ToastApp.showToast("服务端更新，请更新应用版本！");
                }else {
                    main.onCheckFailed(info);
                }
            }
        });
    }
}